const app = getApp();
const db = wx.cloud.database();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        imgList: [],
        fileIDs: [],
        yimiaodata: ['hpv疫苗',"新冠疫苗", "轮状疫苗", "狂犬疫苗", '流感疫苗', "乙肝疫苗", "肺炎疫苗"],
        message: ''
    },

    onChangeyimiao(event) {
        this.setData({
            yimiaotype: event.detail.value,
            yimiaopd: false,
            show: false
        });
    },
    onChangemenzhen(event){
        this.setData({
            menzhen: event.detail
        })
    },
    oncancelyimiao() {
        this.setData({
            yimiaopd: false,
            show: false
        })
    },

    choseyimiao() {
        this.setData({
            yimiaopd: true,
            show: true
        })
    },

    neirong(e) {
        this.setData({
            neirong: e.detail
        })
    },

    onLoad(option) {
        console.log(option)
        this.setData({})
    },

    onChangename(event) {
        this.setData({
            name: event.detail
        })
    },
    onChangefeiyong(event) {
        this.setData({
            feiyong: event.detail
        })
    },
    onChangechangjia(event) {
        this.setData({
            changjia: event.detail
        })
    },
    //上传数据
    publish() {
        const name = this.data.name
        const feiyong = this.data.feiyong
        const changjia = this.data.changjia
        const neirong = this.data.neirong
        const yimiaotype = this.data.yimiaotype
        let d = new Date()
        if (!name) {
            wx.showToast({
                title: '请输入名字',
                icon: 'none'
            })
        } else if (!yimiaotype) {
            wx.showToast({
                title: '请输选择疫苗',
                icon: 'none'
            })
        } else if (!neirong) {
            wx.showToast({
                title: '请输入简介',
                icon: 'none'
            })
        } else if (!feiyong) {
            wx.showToast({
                title: '请输入生产厂家',
                icon: 'none'
            })
        } else if (!changjia) {
            wx.showToast({
                title: '请输入费用',
                icon: 'none'
            })
        } else {
            wx.showLoading({
              title: '发布中',
            })
            let d = new Date();
            db.collection('yimiao').add({
                data: {
                    menzhen:this.data.menzhen,
                    yiyuanuserid:wx.getStorageSync('user')._id,
                    yimiaotype,
                    phone: wx.getStorageSync('user').phone,
                    diqu: wx.getStorageSync('user').diqu,
                    zhanghao: wx.getStorageSync('user').zhanghao,
                    mima: wx.getStorageSync('user').mima,
                    neirong,
                    name,
                    feiyong,
                    changjia,
                    createtime: d.getFullYear() + ' ' + (d.getMonth() + 1) + '/' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds(),
                    time_order: new Date().getTime(),
                },
                success: res => {
                    wx.hideLoading()
                    if (res._id) {
                        wx.showToast({
                            title: '发布成功',
                        })
                        console.log('发布成功', res)
                        wx.navigateTo({
                          url: '../newyiyuan/index',
                        })
                   
                    }
                },
                fail: err => {
                    wx.hideLoading()
                    wx.showToast({
                        icon: 'none',
                        title: '网络不给力....'
                    })
                    console.error('发布失败', err)
                }
            })

        }




    }
})